This version ignores the initial clustering, instead using the bam file from the alignment of all data to the mixed human/mouse reference genomes. For each fastq read identifier, the better alignment score is stored for either genome. The script then iterates through the paired fastq file, partitioning the reads to separate fastq pairs.
These are then run though cellranger count using only the appropriate genomes.
Load human genome split of samples, labeling with hs1..hs4. Output matrix.
Convert to common mouse genes. Re-load into object. Load mouse genome split of samples, labeling with ms1..ms4. Subset overlapping genes. Merge all 8. Normalize and scale. Show clusters labeled by cell; split by sample. Show nCount_RNA also. Determine significantly-different list mouse vs human.
hs1.data=Read10X(data.dir="./hg19/S1/outs/filtered_feature_bc_matrix/")
hs2.data=Read10X(data.dir="./hg19/S2/outs/filtered_feature_bc_matrix/")
hs3.data=Read10X(data.dir="./hg19/S3/outs/filtered_feature_bc_matrix/")
hs4.data=Read10X(data.dir="./hg19/S4/outs/filtered_feature_bc_matrix/")
ms1.data=Read10X(data.dir="./mm10/S1/outs/filtered_feature_bc_matrix/")
ms2.data=Read10X(data.dir="./mm10/S2/outs/filtered_feature_bc_matrix/")
ms3.data=Read10X(data.dir="./mm10/S3/outs/filtered_feature_bc_matrix/")
ms4.data=Read10X(data.dir="./mm10/S4/outs/filtered_feature_bc_matrix/")
#some human gene symbols have underscores (but these are not in geneTrans).
#Substitute a dot so as not to raise an error.
rownames(hs1.data)=gsub("_",".",rownames(hs1.data))
rownames(hs2.data)=gsub("_",".",rownames(hs2.data))
rownames(hs3.data)=gsub("_",".",rownames(hs3.data))
rownames(hs4.data)=gsub("_",".",rownames(hs4.data))
#rename cells
colnames(x=hs1.data) <- paste('hs1',colnames(x=hs1.data),sep="_")
colnames(x=hs2.data) <- paste('hs2',colnames(x=hs2.data),sep="_")
colnames(x=hs3.data) <- paste('hs3',colnames(x=hs3.data),sep="_")
colnames(x=hs4.data) <- paste('hs4',colnames(x=hs4.data),sep="_")
colnames(x=ms1.data) <- paste('ms1',colnames(x=ms1.data),sep="_")
colnames(x=ms2.data) <- paste('ms2',colnames(x=ms2.data),sep="_")
colnames(x=ms3.data) <- paste('ms3',colnames(x=ms3.data),sep="_")
colnames(x=ms4.data) <- paste('ms4',colnames(x=ms4.data),sep="_")
#create objects
hs1=CreateSeuratObject(counts=hs1.data,project="MG",min.cells=5)
hs2=CreateSeuratObject(counts=hs2.data,project="MG",min.cells=5)
hs3=CreateSeuratObject(counts=hs3.data,project="MG",min.cells=5)
hs4=CreateSeuratObject(counts=hs4.data,project="MG",min.cells=5)
ms1=CreateSeuratObject(counts=ms1.data,project="MG",min.cells=5)
ms2=CreateSeuratObject(counts=ms2.data,project="MG",min.cells=5)
ms3=CreateSeuratObject(counts=ms3.data,project="MG",min.cells=5)
ms4=CreateSeuratObject(counts=ms4.data,project="MG",min.cells=5)
#Follow https://satijalab.org/seurat/essential_commands.html
hg=merge(x=hs1,y=c(hs2,hs3,hs4),project="Hg")
mg=merge(x=ms1,y=c(ms2,ms3,ms4),project="Mg")
#at this point we don't need all the original sample objects--can re-create if needed
rm(hs1,hs2,hs3,hs4,hs1.data,hs2.data,hs3.data,hs4.data)
rm(ms1,ms2,ms3,ms4,ms1.data,ms2.data,ms3.data,ms4.data)
#summary of each object
print(hg)
## An object of class Seurat
## 11055 features across 3354 samples within 1 assay
## Active assay: RNA (11055 features)
print(mg)
## An object of class Seurat
## 17255 features across 28237 samples within 1 assay
## Active assay: RNA (17255 features)
Output raw data to data frame. Use conversion table (geneTrans) to translate human symbols to mouse symbols.
#load gene translation table
geneTrans=read.table("geneTrans.txt",sep=",",header=T,stringsAsFactors = F,row.names = 1)
mito.m=grep("^mt",rownames(mg),value=T)
mito.h=grep("^MT-",rownames(hg),value=T)
#turns out, these two vectors (n=13 each) are ordered and match, build an extended translation table
mitoTrans=data.frame(row.names = paste("mm10",mito.m,sep="_"),
Human.Symbol = mito.h,
Homologene_ID = rep(NA,13),
None = rep("yes",13),
Mouse.Symbol = mito.m,
hg19 = paste("hg19",mito.h,sep="_"),
mm10 = paste("mm10",mito.m,sep="_")
)
#extended translation table
xTrans = rbind(geneTrans,mitoTrans)
#extract human raw counts into table
hg.raw=GetAssayData(hg,slot="counts")
#subset rows in geneTrans (not necessary but simpler)
hg.raw=hg.raw[row.names(hg.raw) %in% xTrans$Human.Symbol,] #cut from 13283 to 11364 rows
#translate human symbols to mouse
hg.trans=merge(x=hg.raw,y=xTrans[,c(1,4)],by.x=0,by.y="Human.Symbol",all.x=T)
rownames(hg.trans)=hg.trans$Mouse.Symbol
hg.trans=hg.trans[,!(names(hg.trans) %in% c("Row.names","Mouse.Symbol"))]
#convert matrix back into Seurat object
hm=CreateSeuratObject(hg.trans,project="MG",min.cells=5)
#merge with mouse
hm=merge(x=hm,y=mg,project="HM")
#clean up objects no longer needed
rm(hg,mg,hg.raw,hg.trans,mito.m,mito.h)
#summarize merged object
print(hm)
## An object of class Seurat
## 17477 features across 31591 samples within 1 assay
## Active assay: RNA (17477 features)
Use standard workflow to calculate percent.mito (now all mouse symbols) and visualize by sample.
#filter and normalize merged object
mito.features=grep(pattern="^mt",x=rownames(x=hm),value=T)
percent.mito=Matrix::colSums(x=GetAssayData(object=hm,slot="counts")[mito.features,]) / Matrix::colSums(x=GetAssayData(object=hm,slot='counts'))
hm[['percent.mito']] = percent.mito
VlnPlot(object=hm,features=c("nFeature_RNA","nCount_RNA","percent.mito"),ncol=3)
FeatureScatter(object=hm,feature1 = "nCount_RNA",feature2 = "percent.mito")
FeatureScatter(object=hm,feature1 = "nCount_RNA",feature2 = "nFeature_RNA")
Print summary data before and after subsetting. Then normalize, find variable genes, and scale.
print(hm)
## An object of class Seurat
## 17477 features across 31591 samples within 1 assay
## Active assay: RNA (17477 features)
hm=subset(hm,nFeature_RNA > 200 & nFeature_RNA < 2500 & percent.mito < 0.05) #try > 100 & < 2500 & < .5
print(hm)
## An object of class Seurat
## 17477 features across 9704 samples within 1 assay
## Active assay: RNA (17477 features)
hm=NormalizeData(hm,normalization.method = "LogNormalize",scale.factor=1e4)
hm=FindVariableFeatures(hm,selection.method = 'mean.var.plot',mean.cutoff = c(0.0125,3),dispersion.cutoff = c(0.5,Inf)) #finds 4047 features
length(VariableFeatures(hm))
## [1] 2697
hm=ScaleData(hm,features=rownames(hm),vars.to.regress = c("nCount_RNA","percent.mito")) #this takes a while
## Regressing out nCount_RNA, percent.mito
## Scaling data matrix
Start with PCA and determine how many dimensions are informative.
hm=RunPCA(hm,features=VariableFeatures(hm),verbose=T)
## PC_ 1
## Positive: Ptn, Tsc22d1, Meg3, Spock2, Meis2, Mt3, Slco1a4, Ly6c1, Flt1, Igfbp7
## Pcp4l1, Cldn5, Itm2a, Ly6a, Id1, Ahi1, Igf1r, Atp1a2, Epas1, Snap25
## Il1rapl1, Nrxn3, R3hdm1, Epha5, Gng11, Syt1, Ramp2, Fry, Sorbs2, Ablim1
## Negative: C1qb, Cd74, C1qc, Rpl13a, C1qa, Spp1, Lst1, Cybb, Apoc1, Cx3cr1
## Uba52, Rpl29, Sat1, H2-Ea-ps, C3, Gpr34, Fos, Fcer1g, Alox5ap, Fcgr4
## Trem2, Ptprc, P2ry13, A2m, Cd84, Maf, Csf3r, Adora3, H2-T23, H2-DMb1
## PC_ 2
## Positive: Meis2, Meg3, Il1rapl1, Syt1, Atp1b1, Nrxn3, Epha5, Grin2b, Celf4, Ahi1
## Scg5, Snap25, Negr1, Stmn3, Rtn1, Ndrg4, Plppr4, Snhg11, Arpp21, Bex2
## Ank3, Peg3, Opcml, Gad1, Mt3, Synpr, Pcdh17, Nap1l5, Pcsk2, Eml5
## Negative: Flt1, Ly6c1, Slco1a4, Ly6a, Cldn5, Itm2a, Igfbp7, Ptprb, Pglyrp1, Id1
## Abcb1a, Egfl7, Ctla2a, Klf2, Cxcl12, Adgrf5, Fn1, Slc2a1, Ramp2, Sox18
## Adgrl4, Sgms1, Ablim1, Ahnak, Spock2, Esam, Jcad, Ly6e, Epas1, Abcg2
## PC_ 3
## Positive: Atp1a2, Hexb, Ctsd, Aldoc, Slc1a2, Fyb, Rnase4, Clu, Siglech, Mt2
## Ctss, Id4, Trf, Ctsz, Vsir, Cd52, Mt3, Ptn, Fxyd1, Cd300c2
## Gjb6, Slc7a10, Tmem119, Pla2g7, Aqp4, Car2, Fcgr3, Slc6a11, Unc93b1, Dbi
## Negative: Syt1, Meg3, Snap25, Ndrg4, Celf4, Gad1, Snhg11, Tpm1, Tmsb10, Grin2b
## Stmn3, Nrxn3, Camk2b, Synpr, Plppr4, Atp1a3, Eml5, Epha5, Cd74, Pcp4
## Ano3, Pcsk2, Nrip3, Bcl11a, Snca, Ccsap, C1qtnf4, Spp1, Gad2, Syt6
## PC_ 4
## Positive: Hexb, Ctss, Fyb, Ctsd, Siglech, Rnase4, Tmem119, Vsir, Selplg, C1qa
## Cx3cr1, Fcgr3, C1qc, Cd52, Cd300c2, Ly6e, Mafb, C1qb, Ctsz, Fcer1g
## P2ry12, Ptpn18, Unc93b1, Tgfbr1, Pou2f2, Lyz2, Ighm, Ctsh, Rhoh, P2ry6
## Negative: Atp1a2, Ptn, Dbi, Aldoc, Slc1a2, Clu, Mt2, Car2, Id4, Cryab
## Fxyd1, Mt3, Hes5, Slc7a10, Gjb6, Cnp, Aqp4, Spp1, Slc6a11, Cldn11
## Cd74, H2-Ea-ps, Ermn, Ppp1r14a, Fjx1, Sfxn5, Apoc1, Mlc1, Tspan2, Sept4
## PC_ 5
## Positive: Atp1a2, Aldoc, Mt3, Slc1a2, Mt2, Clu, Id4, Fxyd1, Gjb6, Aqp4
## Slc7a10, Pla2g7, Slc6a11, Fjx1, Hes5, Mlc1, Pdgfrb, Phkg1, Etnppl, Sfxn5
## Mgst1, Itih3, Rarres2, Slc7a11, Gm14964, Dkk3, Hes1, Fabp7, Igfbp2, 2900052N01Rik
## Negative: Cldn11, Ermn, Mog, Tspan2, Tubb4a, Mag, Ugt8a, Mal, Tmem88b, Opalin
## Stmn4, Cnp, Nkx6-2, Mobp, Ppp1r14a, Qdpr, Kctd13, Grb14, C030029H02Rik, Ttll7
## Hapln2, Gjc3, Cryab, Trf, Anln, Kcna1, Enpp2, Dixdc1, Ndrg1, Edil3
print(hm[['pca']],dims=1:5,nfeatures=5,projected=F)
## PC_ 1
## Positive: Ptn, Tsc22d1, Meg3, Spock2, Meis2
## Negative: C1qb, Cd74, C1qc, Rpl13a, C1qa
## PC_ 2
## Positive: Meis2, Meg3, Il1rapl1, Syt1, Atp1b1
## Negative: Flt1, Ly6c1, Slco1a4, Ly6a, Cldn5
## PC_ 3
## Positive: Atp1a2, Hexb, Ctsd, Aldoc, Slc1a2
## Negative: Syt1, Meg3, Snap25, Ndrg4, Celf4
## PC_ 4
## Positive: Hexb, Ctss, Fyb, Ctsd, Siglech
## Negative: Atp1a2, Ptn, Dbi, Aldoc, Slc1a2
## PC_ 5
## Positive: Atp1a2, Aldoc, Mt3, Slc1a2, Mt2
## Negative: Cldn11, Ermn, Mog, Tspan2, Tubb4a
VizDimLoadings(hm,dims=1:2)
DimPlot(hm)
hm=ProjectDim(hm)
## PC_ 1
## Positive: Ptn, Gria2, Zbtb20, Selenow, Sptbn1, Tcf4, Mt1, mt-Atp6, Tsc22d1, Xist
## Meg3, Calm1, Bsg, Rsrp1, Nfib, Atp5j, Sparcl1, Scd2, Pbx1, Elob
## Negative: Tyrobp, C1qb, Cd74, B2m, Aif1, C1qc, Rpl13a, C1qa, Spp1, Lst1
## Cybb, Ftl1, Apoc1, Cx3cr1, Laptm5, Uba52, Rpl29, Sat1, H2-Ea-ps, C3
## PC_ 2
## Positive: Gria2, Ckb, Meis2, Meg3, Dclk1, Ank2, Pcsk1n, Il1rapl1, Syt1, Atp1b1
## Nrxn3, Epha5, Nrxn1, Dlgap1, Grin2b, Celf2, Cadm2, Celf4, Ahi1, Scg5
## Negative: Flt1, Ly6c1, Slco1a4, Ly6a, Cldn5, Itm2a, Igfbp7, Ptprb, Pglyrp1, Id1
## Abcb1a, Egfl7, Ctla2a, Klf2, Cxcl12, Adgrf5, Fn1, Slc2a1, Bsg, Ramp2
## PC_ 3
## Positive: Cst3, Apoe, Cd81, Atp1a2, Hexb, Ctsd, Tsc22d4, Aldoc, Gja1, Plpp3
## Slc1a2, Mt1, Selenop, Glul, Prdx6, Arhgap5, Slc1a3, Ntsr2, Fyb, Rnase4
## Negative: Syt1, Meg3, Snap25, Ndrg4, Celf4, Gad1, Snhg11, Tpm1, Tmsb10, Grin2b
## Stmn3, Nrxn3, Camk2b, Synpr, Plppr4, Atp1a3, Eml5, Epha5, Cd74, Pcp4
## PC_ 4
## Positive: Hexb, Ctss, Fyb, Ctsd, Siglech, Rnase4, Tmem119, Vsir, Selplg, C1qa
## Cx3cr1, Lgmn, Fcgr3, C1qc, Cd52, Csf1r, Cd300c2, Rpl17, Ly6e, Mafb
## Negative: Atp1a2, Ptn, Plpp3, Gpm6b, Htra1, Gja1, Prdx6, Dbi, Aldoc, Ptprz1
## Prnp, Gpr37l1, Scd2, Slc1a3, Slc1a2, Ntsr2, Clu, Sparcl1, Mt2, Atp1b2
## PC_ 5
## Positive: Atp1a2, Gja1, Sparcl1, Aldoc, Mt3, Slc1a2, Mt2, Atp1b2, Plpp3, Clu
## Ntsr2, Mfge8, Slc1a3, Prdx6, Id4, S1pr1, Gm3764, Cxcl14, Gpr37l1, Sox9
## Negative: Cldn11, Ermn, Mog, Tspan2, Tubb4a, Mag, Ugt8a, Plp1, Mal, Tmem88b
## Opalin, Stmn4, Cnp, Nkx6-2, Mobp, Ppp1r14a, Qdpr, Kctd13, Grb14, C030029H02Rik
DimHeatmap(hm,dims=1,cells=500,balanced=T)
DimHeatmap(hm,dims=1:6,cells=500,balanced=T)
hm=JackStraw(hm,num.replicate=100)
hm=ScoreJackStraw(hm,dims=1:20)
JackStrawPlot(hm,dims=1:20)
## Warning: Removed 37760 rows containing missing values (geom_point).
ElbowPlot(hm)
#start clustering
hm=FindNeighbors(hm,dims=1:13) #adjust dims based on plots
## Computing nearest neighbor graph
## Computing SNN
hm=FindClusters(hm,resolution=0.2)
## Modularity Optimizer version 1.3.0 by Ludo Waltman and Nees Jan van Eck
##
## Number of nodes: 9704
## Number of edges: 336771
##
## Running Louvain algorithm...
## Maximum modularity in 10 random starts: 0.9681
## Number of communities: 12
## Elapsed time: 1 seconds
table(Idents(hm))
##
## 0 1 2 3 4 5 6 7 8 9 10 11
## 2315 1707 1637 1123 1022 519 495 263 223 212 141 47
hm=RunTSNE(hm,dims=1:13)
DimPlot(hm,reduction='tsne')
DimPlot(hm,reduction='tsne',split.by='orig.ident')
FeaturePlot(hm,features='Spp1')
FeaturePlot(hm,features='Hexb')
FeaturePlot(hm,features='nCount_RNA')
hm=RunUMAP(hm,dims = 1:13)
DimPlot(hm,reduction='umap')
FeaturePlot(hm,features='Spp1')
FeaturePlot(hm,features='Hexb')
#FeaturePlot(hm,features='nCount_RNA',split.by='orig.ident')
FeaturePlot(hm,features='nCount_RNA')
Use top 2 genes from prior clustering to do this, following
hm.markers=FindAllMarkers(hm,only.pos=T,min.pct = .25,logfc.threshold = .25)
## Calculating cluster 0
## Calculating cluster 1
## Calculating cluster 2
## Calculating cluster 3
## Calculating cluster 4
## Calculating cluster 5
## Calculating cluster 6
## Calculating cluster 7
## Calculating cluster 8
## Calculating cluster 9
## Calculating cluster 10
## Calculating cluster 11
hm.markers %>% group_by(cluster) %>% top_n(10,avg_logFC)
## # A tibble: 120 x 7
## # Groups: cluster [12]
## p_val avg_logFC pct.1 pct.2 p_val_adj cluster gene
## <dbl> <dbl> <dbl> <dbl> <dbl> <fct> <chr>
## 1 0 2.75 0.984 0.212 0 0 Slc1a2
## 2 0 2.53 0.944 0.133 0 0 Aldoc
## 3 0 2.44 0.973 0.243 0 0 Mt2
## 4 0 2.40 0.985 0.292 0 0 Mt3
## 5 0 2.39 0.936 0.123 0 0 Plpp3
## 6 0 2.36 0.931 0.094 0 0 Gja1
## 7 0 2.36 0.883 0.104 0 0 Clu
## 8 0 2.30 0.847 0.068 0 0 Ntsr2
## 9 0 2.30 0.993 0.511 0 0 Mt1
## 10 0 2.19 0.971 0.278 0 0 Slc1a3
## # … with 110 more rows
hm.markers %>% group_by(cluster) %>% top_n(5,avg_logFC) %>% write.csv("Top 5 per cluster.csv",row.names = F)
hm.markers %>% group_by(cluster) %>% top_n(5,avg_logFC) %>% as.data.frame %>% paged_table
Use Spp1 and Hexb to identify cluster number for human and mouse microglia, respectively.
humClus=as.character(subset(hm.markers,gene=="Spp1")$cluster)
print(humClus)
## [1] "1"
musClus=as.character(subset(hm.markers,gene=="Hexb")$cluster)
print(musClus)
## [1] "2" "11"
diffGenes=FindMarkers(hm,ident.1=humClus,ident.2=musClus,min.pct=0.1)
table(sig=diffGenes$p_val_adj <= 0.05, twofold=abs(diffGenes$avg_logFC) >= 1)
## twofold
## sig FALSE TRUE
## FALSE 84 0
## TRUE 1298 230
diffGenes %>% tibble::rownames_to_column() %>% filter(p_val_adj <= 0.05) %>% filter(abs(avg_logFC) >= 1) %>% paged_table
saveRDS(diffGenes,"by_score_diffGenes.rds")
write.table(as.data.frame(diffGenes),"by_score_diffGenes.csv.txt",sep=",",quote=T)
hm.mean=AverageExpression(hm)
## Finished averaging RNA for cluster 0
## Finished averaging RNA for cluster 1
## Finished averaging RNA for cluster 2
## Finished averaging RNA for cluster 3
## Finished averaging RNA for cluster 4
## Finished averaging RNA for cluster 5
## Finished averaging RNA for cluster 6
## Finished averaging RNA for cluster 7
## Finished averaging RNA for cluster 8
## Finished averaging RNA for cluster 9
## Finished averaging RNA for cluster 10
## Finished averaging RNA for cluster 11
head(hm.mean$RNA)
## 0 1 2 3 4 5
## A1bg 0.00000000 0.62931387 0.000000000 0.00000000 0.00000000 0.00000000
## A2m 0.10925748 5.72362778 0.004887344 0.01264058 0.01963727 0.02919465
## Aaas 0.02781175 0.11523591 0.048804827 0.08608053 0.01375711 0.08251127
## Aacs 0.19932879 0.04339625 0.048356781 0.04734966 0.04012529 0.04259827
## Aagab 0.10404891 0.05495286 0.274149181 0.11607317 0.11216276 0.11495272
## Aak1 0.55510168 2.14341997 0.290732859 0.50367291 1.34949972 0.33795351
## 6 7 8 9 10 11
## A1bg 0.00000000 0.04137423 0.00000000 0.00000000 0.00000000 0.00000000
## A2m 0.01082638 0.35174176 0.00000000 0.03127972 0.41174013 0.06228512
## Aaas 0.04486083 0.12787422 0.02423949 0.03685142 0.09139899 0.08199074
## Aacs 0.04910080 0.01342140 0.00000000 0.12360106 0.01550207 0.00000000
## Aagab 0.08208367 0.10658650 0.02631281 0.04734884 0.21889650 0.00000000
## Aak1 0.76748669 0.61815603 0.42917103 0.68831690 0.36737186 0.79225088
write.csv(hm.mean$RNA,"hm cluster averages.csv.txt")
#stash idents
hm[["old.ident"]]=Idents(hm)
#get vector of cell idents
all.cells=Cells(hm)
#split by species
hg.cells=grep("^h",all.cells,value=T)
mm.cells=grep("^m",all.cells,value=T)
#apply new idents
Idents(hm,cells=hg.cells)="Human"
Idents(hm,cells=mm.cells)="Mouse"
table(hm$orig.ident,Idents(hm))
##
## Mouse Human
## hs1 0 434
## hs2 0 317
## hs3 0 592
## hs4 0 384
## ms1 1794 0
## ms2 784 0
## ms3 3342 0
## ms4 2057 0
DimPlot(hm,label=T,repel=T,label.size=8,reduction = "tsne")+NoLegend()
FeaturePlot(hm,features='nCount_RNA',reduction = "tsne")
FeaturePlot(hm,features='nCount_RNA',reduction = "tsne",split.by="ident")